require "Common/define"
require "Logic/Config"
require "Common/functions"
require "Common/Class"

TAManager = {}
TAManager.IsFirstShowSDKLogin = true

-- --重置默认AppId
-- function TAManager.ResetAppId(appId)
--     TA.TABehavior.InitAppId(appId)
-- end

function TAManager.Login()
    local _roleId = tostring(G.serverData["roleInfo"].show.playerId)
    TA.TABehavior.Login(_roleId)

    -- local _loginDic = {}
    -- _loginDic.role_id = playerId
    -- _loginDic.role_name = tostring(G.serverData["roleInfo"].show.name)
    -- TAManager.TrackByDictionary("login_role",_loginDic)
end
function TAManager.TrackByDictionary(eventName,propertiesDic)
    if propertiesDic == nil or table.getn(propertiesDic) == 0 then
        propertiesDic = {}
    end
    
    propertiesDic.uid = logicMgr.LoginManager.user_id
    propertiesDic.server_id = AppConst.ServerId
    propertiesDic.channel = Config.SDK_Channel
    propertiesDic.sub_channel = Config.SDK_SubChannel
    propertiesDic.main_stage_id = logicMgr.MapManager.GetCurrentStage()
    propertiesDic.bundle_id = logicMgr.SDKManager.GetAPKName()
    if G.serverData["roleInfo"] and G.serverData["roleInfo"]["show"] and G.serverData["roleInfo"]["show"]["name"] then
        propertiesDic.role_name = G.serverData["roleInfo"]["show"]["name"]
    else
        propertiesDic.role_name = ""
    end
    local _propertiesJson = getJsonStr(propertiesDic)
    if not string.IsNilOrEmpty(_propertiesJson) then
        TAManager.CalibrateTime()

        TA.TABehavior.Track(eventName,_propertiesJson)
    end
end

function TAManager.UserSet(property,value)
    TA.TABehavior.UserSet(property, value)
end

--属性值累加
function TAManager.UserAdd(property, value)
    TA.TABehavior.UserAdd(property, value)
end

function TAManager.TrackFirstEvent(eventName,firstCheckId,propertiesDic)
    TAManager.CalibrateTime()
    TA.TABehavior.TrackFirstEvent(eventName,firstCheckId,propertiesDic)
end

--同步数数sdk时间
function TAManager.CalibrateTime()
    local serverTime = tonumber(HelperFunc.GetServerTime(false))
    if serverTime ~= 0 then
        TA.TABehavior.CalibrateTime(serverTime)
    end
end

function TAManager.SetSeverCountProperty()
    loginHandler.HttpGetServerUserList( function(data) 
        local count = 1
        if data.recordList then
            count = #data.recordList
        end
        TAManager.serverCount = count
        -- logError("服务器>>>>>>>>>>>>>>>>>>>>>>>>>:" .. TAManager.serverCount)
        TAManager.UserSet("server_count", TAManager.serverCount)

        if count == 1 then
            TAManager.UserSet("first_roleid", G.serverData["roleInfo"].show.playerId)
        end
    end )
end
local item_map = {
    -- ["1"]=1,["3"]=3,["6"]=6,["7"]=7,["8"]=8,["9"]=9,["13"]=13,["14"]=14,["15"]=15,["18"]=18,["19"]=19,["20"]=20,
    -- ["25"]=25,["29"]=29,
    -- ["200001"]=200001,["200002"]=200002,["200003"]=200003,["200004"]=200004,["200005"]=200005,["200083"]=200083,
    -- ["200006"]=200006,["200008"]=200008,["200013"]=200013,["200021"]=200021,["200081"]=200081,["200082"]=200082,
    -- ["200024"]=200024,["200046"]=200046,["200120"]=200120,["200140"]=200140,["200160"]=200160,["200301"]=200301,
    -- ["200302"]=200302,["200303"]=200303,["200304"]=200304,["200305"]=200305,["200306"]=200306,["200311"]=200311,
    -- ["200312"]=200312,["200313"]=200313,["200314"]=200314,["200315"]=200315,["200316"]=200316,["200321"]=200321,
    -- ["200322"]=200322,["200323"]=200323,["200324"]=200324,["200325"]=200325,["200326"]=200326,["200331"]=200331,
    -- ["200332"]=200332,["200333"]=200333,["200334"]=200334,["200335"]=200335,["200336"]=200336,

    ["6"]=6,["7"]=7,["13"]=13,["14"]=14,["15"]=15,["16"]=16,["18"]=18,["19"]=19,["20"]=20,["24"]=24,["25"]=25,
    ["29"]=29,["31"]=31,["32"]=32,["33"]=33,["34"]=34,["35"]=35,
    ["200028"]=200028,["200001"]=200001,["200002"]=200002,["200003"]=200003,["200004"]=200004,["200005"]=200005,
    ["200006"]=200006,["200013"]=200013,["200021"]=200021,["200023"]=200023,["200046"]=200046,["200029"]=200029,
}

--道具类型判定 >为减少, <为增加
function TAManager.TrackItemEvent(item_uniqueid,itemModelid,currentCount,lastCount,sourceCode,params)
    if sourceCode == nil or sourceCode == -1 then
        return
    end
    --背包类型道具埋点
    local _itemChangeDic = {}
    _itemChangeDic.source = tonumber(sourceCode)
    _itemChangeDic.item_uniqueid = tostring(item_uniqueid)
    _itemChangeDic.item_id = tostring(itemModelid)
    _itemChangeDic.current_count = tonumber(tostring(currentCount))
    _itemChangeDic.last_count = tonumber(lastCount)
    _itemChangeDic.delta_count = _itemChangeDic.current_count - _itemChangeDic.last_count
    -- local _params = ""
    -- if params then
    --    _params = string.gsub(params,",","_")
    -- end
    -- _itemChangeDic.params = _params
    
    local _itemEventName
    if tonumber(sourceCode) / 1000 < 1 then
        _itemEventName = "get_item"
    else    
        _itemEventName = "consume_item"
    end
	TAManager.TrackByDictionary(_itemEventName,_itemChangeDic)
    
    if item_map[_itemChangeDic.item_id] then
        TAManager.UserSet("current_itemid_" .. _itemChangeDic.item_id, _itemChangeDic.current_count)
    end
end

function TAManager.LogoutAndExit()
    TA.TABehavior.LoginOut()
    TAManager.TrackByDictionary("ta_app_end", {})
    TAManager.serverCount = nil
end

--获取英雄来源
TAManager.GetHeroSource = 
{
    Diamond_Ten = "diamond_ten",        --红魔石十连抽
    Diamond_One = "diamond_one",        --红魔石单抽 
    Friendly_Ten = "friendly_ten",      --友情十连抽
    Friendly_One = "friendly_one",      --友情单抽 
    Pond_One = "pond_one",              --许愿池单抽
    Pond_Ten = "pond_ten",              --许愿池十连
    MainStage = "main_stage",           --主线关卡
    Bag = "bag",                        --背包
}

return TAManager